package com.vidmt.api.modules.sys.auth;

public abstract class DefaultPwdStrategy implements PwdStrategy {
	@Override
	final public String encryptPassword(String plainpwd, String key) {
		String keystr = fmtKey(key, plainpwd);
		if (keystr == null) {
			keystr = "";
		}
		return String.format("%s#%s#%s", getAlg(), fmtKey(key, plainpwd), getCyphered(key, plainpwd));
	}

	@Override
	public boolean valid(String plainpwd, String savedPassword) {
		String[] arr = savedPassword.split("#");
		return getCyphered(defmtKey(arr[1]), plainpwd).equals(arr[2]);
	}

	abstract protected String getCyphered(String rawkey, String plainpwd);

	protected String fmtKey(String rawkey, String plainpwd) {
		return rawkey;
	}

	protected String defmtKey(String fmtkey) {
		return fmtkey;
	}
}
